/*
 * PanHistograma.java
 *
 * Created on 27 de julio de 2008, 08:13 PM
 */

package chromaticitymoments;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Vector;

/**
 *
 * @author  Frank
 */
public class PanHistograma extends javax.swing.JPanel {

    private Point pInicial;
    private Point pFinalX;
    private Point pFinalY;
    private String nombreHistograma;
    private double[] vNumeroTonos;

   
    private double espacioX;
    /** Creates new form PanHistograma */
    public PanHistograma(String NombreHistograma) {
        initComponents();
        this.setBackground(Color.WHITE);
        this.vNumeroTonos=null;
        this.pInicial=new Point(0,0);
        pFinalX=new Point(0,0);
        pFinalY=new Point(0,0);
        this.nombreHistograma=NombreHistograma;
    }
   
    public void paint(Graphics g)
    {
        super.paint(g);
        
        if(this.vNumeroTonos!=null)
        {
            Dimension d=this.getSize();
            pInicial.x=20;
            pInicial.y=d.height-20;
            pFinalY.x=pInicial.x;
            pFinalY.y=20;
            pFinalX.x=d.width-20;
            pFinalX.y=pInicial.y;

            g.drawLine(pInicial.x,pInicial.y,pFinalX.x,pFinalX.y);
            g.drawLine(pInicial.x,pInicial.y,pFinalY.x,pFinalY.y);
            this.espacioX=(this.pFinalX.x-this.pInicial.x)/256.0;
            int alturaMaxima=d.height-40;
            double maxTono=this.getMaximoTono();
            int alturaTono;
            double posX=pInicial.x;
            g.setColor(Color.BLUE);
            for(int i=0;i<256;i++)
            {
                posX=posX+espacioX;
                alturaTono=(int)(this.vNumeroTonos[i]/maxTono*alturaMaxima);
                g.drawLine((int)posX,pInicial.y,(int)posX,pInicial.y-alturaTono);
            }
            g.drawString("0",pInicial.x-10,pInicial.y+10);
            g.drawString(String.valueOf((int)maxTono),pFinalY.x-10,pFinalY.y);
            g.drawString("255",pFinalX.x,pFinalX.y+10);
            g.setColor(Color.BLACK);
            g.drawString(nombreHistograma,pInicial.x+10,pInicial.y+15);
            
        }
        
    }
    
    public double getMaximoTono()
    {
        double max=-1;
        for(int i=0;i<256;i++)
        {
            if(this.vNumeroTonos[i]>max)
            {
                max=vNumeroTonos[i];
            }
        }
        return max;
    }
    
     public double[] getVNumeroTonos() {
        return vNumeroTonos;
    }

    public void setVNumeroTonos(double[] vNumeroTonos) {
        this.vNumeroTonos = vNumeroTonos;
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 221, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 162, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
